package com.toulouseculture;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.toulouseculture.bo.CinemaPlace;
import com.toulouseculture.bo.CulturePlace;
import com.toulouseculture.bo.LibraryPlace;
import com.toulouseculture.bo.MuseumPlace;
import com.toulouseculture.bo.TheaterPlace;
import com.toulouseculture.controller.CulturePlaceController;

public class CulturePlaceMapsActivity extends MapActivity {

	private MapView mapView;

	GeoPoint capitolePoint = CulturePlaceController.newGeoPoint(43.604402f, 1.443586f);

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		// map controller
		MapController mapController = mapView.getController();

		mapController.setZoom(17);

		// get the row
		Integer placeId = null;
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			placeId = new Long(extras.getLong(CulturePlaceController.CULTURE_PLACE_ID)).intValue();
		}

		if (placeId.equals(CulturePlace.CINEMA_ID) || //
				placeId.equals(CulturePlace.LIBRARY_ID) || //
				placeId.equals(CulturePlace.MUSEUM_ID) || //
				placeId.equals(CulturePlace.THEATER_ID)) {
			// all of one type
			displayAllCulturePlaceType(placeId);
		} else {
			// one place

			CulturePlaceController controller = CulturePlaceController.getInstance();

			CulturePlace currentCulturePlace = controller.getCulturePlaceById(placeId);

			displayCulturePlace(currentCulturePlace);
		}
	}

	protected void displayAllCulturePlaceType(Integer culturePlaceType) {

		GeoPoint centerGeoPoint = capitolePoint;

		// map controller
		MapController mapController = mapView.getController();

		// list of overlays
		List<Overlay> mapOverlays = mapView.getOverlays();
		mapOverlays.clear();

		// create overlay cultureplace
		Drawable currentDrawable = null;
		List<? extends CulturePlace> culturePlaces = null;

		if (culturePlaceType.equals(CulturePlace.CINEMA_ID)) {
			currentDrawable = this.getResources().getDrawable(R.drawable.map_cinema);
			culturePlaces = CulturePlaceController.getInstance().getAllCinemaPlace();

		} else if (culturePlaceType.equals(CulturePlace.LIBRARY_ID)) {
			currentDrawable = this.getResources().getDrawable(R.drawable.map_library);
			culturePlaces = CulturePlaceController.getInstance().getAllLibraryPlace();

		} else if (culturePlaceType.equals(CulturePlace.MUSEUM_ID)) {
			currentDrawable = this.getResources().getDrawable(R.drawable.map_museum);
			culturePlaces = CulturePlaceController.getInstance().getAllMuseumPlace();

		} else if (culturePlaceType.equals(CulturePlace.THEATER_ID)) {
			currentDrawable = this.getResources().getDrawable(R.drawable.map_theater);
			culturePlaces = CulturePlaceController.getInstance().getAllTheaterPlace();
		}

		CulturePlaceItemizedOverlay currentPlaceItemizedOverlay = new CulturePlaceItemizedOverlay(currentDrawable, this);

		mapController.setCenter(centerGeoPoint);

		// create item for each
		for (CulturePlace culturePlace : culturePlaces) {

			OverlayItem overlayitemStartPoint = new OverlayItem(culturePlace.getGeoPoint(),//
					culturePlace.getName(), //
					culturePlace.getNoStreet() + " " + culturePlace.getAddress() + "\n" + //
							culturePlace.getPhone());
			currentPlaceItemizedOverlay.addOverlay(overlayitemStartPoint);

			mapOverlays.add(currentPlaceItemizedOverlay);
		}

	}

	protected void displayCulturePlace(CulturePlace culturePlace) {

		GeoPoint currentGeoPoint = culturePlace.getGeoPoint();

		// map controller
		MapController mapController = mapView.getController();

		// list of overlays
		List<Overlay> mapOverlays = mapView.getOverlays();
		mapOverlays.clear();

		// create overlay cycle
		Drawable currentDrawable = null;

		if (culturePlace instanceof CinemaPlace) {
			currentDrawable = this.getResources().getDrawable(R.drawable.map_cinema);
		} else if (culturePlace instanceof LibraryPlace) {
			currentDrawable = this.getResources().getDrawable(R.drawable.map_library);
		} else if (culturePlace instanceof MuseumPlace) {
			currentDrawable = this.getResources().getDrawable(R.drawable.map_museum);
		} else if (culturePlace instanceof TheaterPlace) {
			currentDrawable = this.getResources().getDrawable(R.drawable.map_theater);
		}

		CulturePlaceItemizedOverlay currentPlaceItemizedOverlay = new CulturePlaceItemizedOverlay(currentDrawable, this);

		mapController.setCenter(currentGeoPoint);

		OverlayItem overlayitemStartPoint = new OverlayItem(currentGeoPoint,//
				culturePlace.getName(), culturePlace.getNoStreet() + " " + culturePlace.getAddress());
		currentPlaceItemizedOverlay.addOverlay(overlayitemStartPoint);

		mapOverlays.add(currentPlaceItemizedOverlay);

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}